<?php 
    echo $this->headScript()->prependFile($this->basePath() . '/js/accounting.min.js', 'text/javascript');
?>
<div class="submit">
    <div class="submit_container">
        <?php 
            // Product id hidden field
            echo sprintf('<input type="hidden" name="product_id" value="%s">', $this->productId);
        ?>
        <div id="product_price">
            <?php 
                // Compute the default price
                $defaultPrice = $this->price['value'];
        
                foreach ($this->attributes as $attribute)
                    foreach ($attribute as $attributeValue)
                        if ($attributeValue['is_default'])
                            $defaultPrice += $attributeValue['price'];
                
                echo $this->currencyFormat($this->price['value'], $this->price['currency'], $this->locale);
            ?>
        </div>
        <div id="product_quantity">
            <?php echo sprintf('<div>%s</div>', $this->translate('quantity', 'catalog', $this->locale)); ?>
            <input type="text" id="quantity" name="quantity" value="1">
        </div>
    </div>
<hr>
<?php echo sprintf('<input type="submit" class="button submit_button" value="%s">', $this->translate('add to basket', 'catalog', $this->locale)) ?>
</div>
<script type="text/javascript">
    // Form submit checks
    $(document).ready(function() {
        $('form').submit(function() {
            var quantity = $('#quantity').val();

            if (!$.isNumeric(quantity) || ($.isNumeric(quantity) && parseInt(quantity) <= 0)) 
                $('#quantity').val('1');
        });
    });
            
    <?php 
        // Get the currency symbol
        $formatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);
    
        // Prepare JSON attributes and price maps
        echo sprintf('var attributes = %s;', $this->json($this->attributes));
        echo sprintf('var price = %s;', $this->json($this->price));
        echo sprintf('var currency = \'%s\';', $formatter->getSymbol(NumberFormatter::CURRENCY_SYMBOL));       
    ?>

    // Selected product attributes
    var selectedAttributes = {};
    
    $(document).ready(function () {
    	$(document).bind('product_attribute_value_changed', function(e, attributeValueId) {
            var priceWithAttributes = parseFloat(price.value);
        	
    	    // Find out the attribute price
            for (var attributeType in attributes) {
                for (var attributeValue in attributes[attributeType]) {
                    if (attributes[attributeType][attributeValue].id == attributeValueId) {
                    	selectedAttributes[attributeType] = attributes[attributeType][attributeValue];
                    	
                        break;
                    }
                }
            }

            for (var attribute in selectedAttributes) 
            	priceWithAttributes += parseFloat(selectedAttributes[attribute].price);        
                    	 
    	    // Update the price
    	    var formattedPrice = accounting.formatMoney(priceWithAttributes, { symbol : currency, format : '%s %v' });

    	    $('#product_price').text(formattedPrice);
        });
    });
</script>